package br.pucrs.lognplay.faces.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.DualListModel;

import br.pucrs.lognplay.entity.Client;
import br.pucrs.lognplay.entity.LogSource;
import br.pucrs.lognplay.entity.Tag;
import br.pucrs.lognplay.entity.User.Role;
import br.pucrs.lognplay.service.ClientManagementService;
import br.pucrs.lognplay.service.LogSourceManagementService;
import br.pucrs.lognplay.service.TagManagementService;

@ManagedBean
@ViewScoped
public class TagManagementBean extends Bean {

	private Tag tag;

	private List<Client> clients;

	private DualListModel<LogSource> logSources;

	@EJB
	private TagManagementService tagManagementService;

	@EJB
	private ClientManagementService clientManagementService;

	@EJB
	private LogSourceManagementService logSourceManagementService;

	public Tag getTag() {
		try {
			if (tag == null) {
				String tagId = getParameterValue("tagId");

				if (tagId == null) {
					Role loggedUserRole = getLoggedUser().getRole();

					if (loggedUserRole.equals(Role.MASTER_USER)) {
						tag = new Tag(UUID.randomUUID());
					} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
						tag = new Tag(UUID.randomUUID());
						tag.setClient(getLoggedUser().getClient());
					}
				} else {
					tag = tagManagementService.getTagById(tagId);
				}
			}

			return tag;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public void setTag(Tag tag) {
		this.tag = tag;
	}

	public boolean isClientRendered() {
		Role loggedUserRole = getLoggedUser().getRole();

		if (loggedUserRole.equals(Role.MASTER_USER)) {
			return true;
		} else {
			return false;
		}
	}

	public List<Client> getClients() {
		try {
			if (clients == null) {
				clients = clientManagementService.listAllClients();
			}

			return clients;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public void handleClientChange() {
		tag.getLogSources().clear();
		logSources.getSource().clear();
		logSources.getTarget().clear();
	}

	public DualListModel<LogSource> getLogSources() {
		try {
			if (logSources == null) {
				logSources = new DualListModel<LogSource>();
			}

			if (logSources.getSource().isEmpty() && logSources.getTarget().isEmpty()) {
				Client tagClient = tag.getClient();

				if (tagClient != null) {
					List<LogSource> clientLogSources = logSourceManagementService.listLogSourcesByClientId(tagClient.getId());
					clientLogSources.removeAll(tag.getLogSources());
					logSources.setSource(new ArrayList<>(clientLogSources));
					logSources.setTarget(new ArrayList<>(tag.getLogSources()));
				}
			}

			return logSources;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}

	public void setLogSources(DualListModel<LogSource> logSources) {
		this.logSources = logSources;
	}

	public String save() {
		try {
			tag.getLogSources().clear();
			tag.getLogSources().addAll(logSources.getTarget());
			tagManagementService.mergeTag(tag);
			addInformationMessage("The changes have been saved", null);
			return null;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}
}
